﻿using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Pay.Service.Service {
    public class ShippingAddressService : ServiceBase, IShippingAddressService {
        private IUnitOfWork _iUnitOfWork;
        public ShippingAddressService(IUnitOfWork iUnitOfWork)
                     : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
        }

        public List<shippingaddress_dto> GetByUser(long user_id) {
            var shippingaddresses = context.ShippingAddresses.Where(c => c.MerchantId == user_id).FromCache<ShippingAddress, shippingaddress_dto>();
            return shippingaddresses;
        }
        public shippingaddress_dto GetGegionIdByUserId(string UserId, string AddressId) {

            var adresslist = GetByUser(Convert.ToInt64(UserId));
            var addressmodel = new shippingaddress_dto();
            if (AddressId == null || AddressId == "") {
                if (adresslist != null && adresslist.Count > 0) {
                    var defaltmodel = adresslist.Where(x => x.is_default == true).FirstOrDefault();
                    if (defaltmodel != null)
                        addressmodel = defaltmodel;
                    else
                        addressmodel = adresslist[0];
                } else {
                    addressmodel = null;
                }

            } else
                addressmodel = GetById(Convert.ToInt64(AddressId));

            return addressmodel;
        }
        public shippingaddress_dto GetById(long id) {
            var shippingaddresses = context.ShippingAddresses.Where(c => c.Id == id);
            return shippingaddresses.FromCacheFirstOrDefault<ShippingAddress, shippingaddress_dto>();
        }

        public int Delete(long Id) {
            int result = -1;
            var model = context.ShippingAddresses.FindById(Id);
            if (model.IsDefault) {
                result = -2;
            } else {
                context.ShippingAddresses.Remove(model);
                result = _iUnitOfWork.Commit();
            }
            return result;
        }

        public int AddAddress(shippingaddress_dto dto) {
            var model = dto.ToDtoObject<ShippingAddress>();
            if (dto.id > 0) {
                var shipping = context.ShippingAddresses.FirstOrDefault(c => c.Id == dto.id);
                shipping.Address = model.Address;
                shipping.RealName = model.RealName;
                shipping.RegionId = model.RegionId;
                shipping.ShipTo = model.ShipTo;
                shipping.IdCard = model.IdCard;
                shipping.CardNegative = model.CardNegative;
                shipping.CardPositive = model.CardPositive;
            } else {
                var shipping = context.ShippingAddresses.FirstOrDefault(c => c.MerchantId == dto.merchant_id);
                if (shipping == null)
                    model.IsDefault = true;
                context.ShippingAddresses.Add(model);
            }

            int result = _iUnitOfWork.Commit();
            return result;
        }
        public int Update(shippingaddress_dto dto) {

            var model = context.ShippingAddresses.FindById(dto.id);
            model.IsDefault = dto.is_default;

            int result = _iUnitOfWork.Commit();
            return result;
        }

        public int Add(shipping_address_dto dto) {
            if (dto.city_id == 0 && dto.area_id == 0)
                throw new ServerErrorException("没有省市区");
            //一定要有一个默认地址
            var item = context.ShippingAddresses.FirstOrDefault(c => c.MerchantId == dto.user_id && c.IsDefault);
            if (item != null && dto.is_default) item.IsDefault = false;
            if (item == null) dto.is_default = true;

            var model = dto.ToDtoObject<ShippingAddress>();
            model.RegionId = dto.area_id > 0 ? dto.area_id : dto.city_id;

            context.ShippingAddresses.Add(model);
            int result = _iUnitOfWork.Commit();
            return result;
        }

        public void Insert(shipping_address_dto dto) {
            if (dto.city_id == 0 && dto.area_id == 0)
                throw new ServerErrorException("没有省市区");
            //一定要有一个默认地址
            var item = context.ShippingAddresses.FirstOrDefault(c => c.MerchantId == dto.user_id && c.IsDefault);
            if (item == null) dto.is_default = true;

            var model = dto.ToDtoObject<ShippingAddress>();
            model.RegionId = dto.area_id > 0 ? dto.area_id : dto.city_id;

            context.ShippingAddresses.Add(model);
        }

        public int Update(shipping_address_dto dto) {
            if (dto.city_id == 0 || dto.area_id == 0)
                throw new ServerErrorException("没有省市区");
            var model = context.ShippingAddresses.FindById(dto.id);
            var old = model.IsDefault;
            var item = context.ShippingAddresses.FirstOrDefault(c => c.MerchantId == dto.user_id && c.IsDefault);
            if (item != null && dto.is_default) {
                if (item.Id != dto.id)
                    item.IsDefault = false;
            } else dto.is_default = old;

            dto.ToDtoObject<ShippingAddress>(model);
            model.RegionId = dto.area_id > 0 ? dto.area_id : dto.city_id;

            int result = _iUnitOfWork.Commit();
            return result;
        }

        public int UpdateDefault(long id, long merchant_id) {
            var item = context.ShippingAddresses.FirstOrDefault(c => c.MerchantId == merchant_id && c.IsDefault);
            if (item != null) item.IsDefault = false;
            var model = context.ShippingAddresses.FindById(id);
            model.IsDefault = true;
            int result = _iUnitOfWork.Commit();
            return result;
        }

        public shipping_address_dto Get(long id) {
            return context.ShippingAddresses.Where(c => c.Id == id).FromCacheFirstOrDefault(query => {
                var model = query.FirstOrDefault();
                if (model == null)
                    throw new ServerErrorException("地址不存在");
                var result = model.ToDtoObject<shipping_address_dto>();
                return result;
            });
        }
        public shipping_address_dto GetByAddressId(long id) {
            return context.ShippingAddresses.FindById(id).ToDtoObject<shipping_address_dto>();
        }
        public shipping_address_dto GetByDefault(long user_id) {
            var model = context.ShippingAddresses.FirstOrDefault(c => c.MerchantId == user_id && c.IsDefault);
            if (model != null) { return model.ToDtoObject<shipping_address_dto>(); } else return null;
        }
        public shippingaddress_dto GetDefaultAddress(long merchant_id) {
            var model = context.ShippingAddresses.FirstOrDefault(c => c.MerchantId == merchant_id && c.IsDefault);
            return model != null ? model.ToDtoObject<shippingaddress_dto>() : null;
        }
        public IEnumerable<shipping_address_dto> GetAddressByUser(long user_id) {
            return context.ShippingAddresses.Where(c => c.MerchantId == user_id).FromCache(query => {
                var result = query.ProjectTo<ShippingAddress, shipping_address_dto>();
                if (result == null)
                    throw new ServerErrorException("地址不存在");
                return result;
            });
        }
        public int DeleteAdress(long id) {
            int result = 0;
            var address = context.ShippingAddresses.FirstOrDefault(c => c.Id == id);
            if (address.IsDefault) throw new ServerErrorException("默认地址不能删除");
            context.ShippingAddresses.Remove(id);
            result = _iUnitOfWork.Commit();
            return result;
        }
    }
}
